package com.huadi.ssm.Controller;

import com.huadi.ssm.Service.EvidenceService;
import com.huadi.ssm.Service.FireService;
import com.huadi.ssm.Service.UprecordService;
import com.huadi.ssm.Service.UserService;
import com.huadi.ssm.po.Evidence;
import com.huadi.ssm.po.Uprecord;
import com.huadi.ssm.po.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/uprecord")
public class UprecordController {
@Autowired
@Qualifier("UprecordImpl")
    private UprecordService uprecordService;

@Autowired
@Qualifier("FireImpl")
private FireService fireService;

@Autowired
@Qualifier("UserServiceImpl")
private UserService userService;

@Autowired
@Qualifier("EvidenceServiceImpl")
private EvidenceService evidenceService;

@RequestMapping("/alluprecord")
    public String alluprecord(Model model,HttpSession session){
    User user= (User) session.getAttribute("user");

    if(user.getUserdepartment().equals("中心部门")){
    List<Uprecord>  uprecord=uprecordService.findAllUprecord();
    model.addAttribute("uprecord",uprecord);
    return "allUprec";}
    else{
        List<Uprecord> uprecord=uprecordService.findAllUprecordForFireman(user.getUserId());
        model.addAttribute("uprecord",uprecord);
return "alluprecordForFireman";
    }


}
//跳转到申请提交物证的界面
@RequestMapping("/toadduprecord")
public String toadduprecord(){return "adduprecord";}


//输入信息并检查，
    @RequestMapping("/adduprecord")
    public String adduprecord(Uprecord uprecord, HttpSession session){
    User user=userService.findUserByUserId(uprecord.getSubmitterId());
    if((fireService.findFireById(uprecord.getFireId())!=null)){
        uprecordService.addUprecord(uprecord);
        return "redirect:/uprecord/alluprecord";
    }else{//返回错误信息：火灾不存在，请重新输入
        return "adduprecord";
    }
}

//跳转到提交申请审核界面
    @RequestMapping("/tocheckUR")
    public String checkUR(){return "checkUR";}


/*审核申请
    1:找到审核的的中心人员的ID和姓名
    2：通过记录中的物证ID在申请表中找到
    3：审核通过后自动在物证表中加入该信息
 */

@RequestMapping("/checkup")
public String checkup(int evidenceUpId,String judge,String judgeReason,HttpSession session){
    User user= (User) session.getAttribute("user");

    uprecordService.checkUprecord(evidenceUpId,user.getUserId(),user.getUsername(),judge,judgeReason);
    if(judge.equals("通过")){
    Uprecord uprecord=uprecordService.findUprecordById(evidenceUpId);
    evidenceService.addEvidence(uprecord.getEvidenceType(),uprecord.getSubmitterId(),uprecord.getFireId(),uprecord.getEvidenceDescribe());}

    return "redirect:/uprecord/alluprecord";

}
}
